<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
    <!-- table -->
    <!-- 旧件仓库维护 - 旧件仓库维护新增 -->
    <FormPanel ref="formPanel1" :list="formList" :cols="2" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 1rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <Vehicle @close="close" @save="save"></Vehicle>
    </BaseDialog>
    </Spin>
  </div>
</template>
<script>
import Vehicle from '@/pages/claimDemage/tools/vehicle.vue';
import { getPartType,getProvinceList, getCityList } from '@/api/dictionary/index.js';
import { getListByPage, newSave, piliangqiyong, piliangtingyong, excelExport, editSave } from '@/api/claim/factory/OldPartWarehouseMaintain';
import { IDNumber, zipCode,phone,email,testPW } from '@/utils/validate'
import { getProvince, getCitys, getDistrict, getRegion } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
export default {
  // mixins: [dictionary],
  components: {
    Vehicle
  },
  data() {
    return {
      loading: false,
      dialogTitle: '配件选择',
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      // topFilterInit: this.topFilterInitFun(),
      // fetchApi: getUser,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      saveData: null
    };
  },
  created() {
    //城市
    // this.city();
    //省份
    this.getProvince();
  },
  methods: {
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel1.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.loading = true;
        let res = await newSave(this.saveData);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          // this.closeDialog();
          this.loading = false;
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', {visible: false, data: this.selectedRows, type: 'close'});
    },
    close(){
      this.visible = false;
    },
    save(obj) {
      this.formList.find(x => x.fieldName == 'partCode').initialValue = obj[0].partCode;
      this.formList.find(x => x.fieldName == 'partName').initialValue = obj[0].partName;
      this.visible = false;
    },
    async getProvince() {
      const res = await getProvince();
      this.formList.find(item => item.fieldName === 'provinceCode').itemList = res.data.map(x => ({ value: x.regionId, text: x.regionName }));
    },
    async provinceChange_linker(regionId) {
      this.clearFormItem(this.formList.find(item => item.fieldName === 'cityCode'));
      console.log(regionId);
      if (!regionId) return;
      const res = await getCitys(regionId);
      this.formList.find(item => item.fieldName === 'cityCode').itemList = res.data.map(x => ({ value: x.regionId, text: x.regionName }));
    },
    clearFormItem(item) {
      item.initialValue = '';
      item.itemList = [];
    },

    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '仓库代码',
          fieldName: 'whCode',
          placeholder: '请输入仓库代码...',
          rules: [{required: true, message: '请输入仓库代码...', trigger: 'change'}]
        },
        {
          type: 'INPUT',
          label: '仓库名称',
          fieldName: 'whName',
          placeholder: '请输入仓库名称...',
          rules: [{required: true, message: '请输入仓库名称...', trigger: 'change'}]
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'cPerson',
          placeholder: '请输入联系人...',
        },
        {
          type: 'INPUT',
          label: '联系电话',
          fieldName: 'cTel',
          placeholder: '请输入联系电话...',
          rules: [{ trigger: 'blur', validator: phone }]
        },
        {
          type: 'INPUT',
          label: '邮编',
          fieldName: 'postCode',
          placeholder: '请输入邮编...',
          rules: [{ trigger: 'blur', validator: zipCode }]
        },
        {
          type: 'INPUT',
          label: '邮箱',
          fieldName: 'eMail',
          placeholder: '请输入邮箱...',
          rules: [{ trigger: 'blur', validator: email }]
        },
        {
          type: 'SELECT',
          label: '省份',
          filterable: true,
          fieldName: 'provinceCode',
          placeholder: '请选择省份...',
          itemList: [],
          change: val => {
            this.provinceChange_linker(val);
          },
        },
        {
          type: 'SELECT',
          label: '城市',
          // filterable: true,
          fieldName: 'cityCode',
          placeholder: '请选择城市...',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'whStatus',
          filterable: true,
          placeholder: '请选择状态...',
          initialValue: 1,
          itemList: [{text: '启用', value: 1}, {text: '禁用', value: 0}],
        },
        {
          type: 'TEXT_AREA',
          label: '地址',
          fieldName: 'whAddress',
          rows: 2,
          selfCols: 2, // 元素占据几行
          initialValue: '',
          placeholder: '请输入地址...'
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          rows: 2,
          selfCols: 2, // 元素占据几行
          placeholder: '请输入备注...'
        },
      ];
    }

  }
};
</script>

<style lang="less" scoped></style>
